<?php 

function kode_klaim(){
	$panjang=4;
	$thun_skrang=date('Y');
	$bulan_skrang=date('m');
	$sql="SELECT max(substr(id_klaim,7,4)) as banyak FROM klaim where year(tgl_input)='$thun_skrang' and month(tgl_input)='$bulan_skrang'";
	$qry	= mysql_query($sql);
 	$row	= mysql_fetch_array($qry); 
	
	$angka=$row['banyak'];
	
	//echo $angka.'<br>';
	
	$angka++;
 	$angka	=strval($angka); 
	$tmp	="";
 	for($i=1; $i<=($panjang-strlen($angka)); $i++) {
		$tmp=$tmp."0";	
	}
	return $thun_skrang.$bulan_skrang.$tmp.$angka;
}

function kdauto($tabel, $inisial){
	$struktur	= mysql_query("SELECT * FROM $tabel");
	$field		= mysql_field_name($struktur,0);
	$panjang	= mysql_field_len($struktur,0);

 	$qry	= mysql_query("SELECT max(".$field.") FROM ".$tabel);
 	$row	= mysql_fetch_array($qry); 
 	if ($row[0]=="") {
 		$angka=0;
	}
 	else {
 		$angka		= substr($row[0], strlen($inisial));
 	}
	
 	$angka++;
 	$angka	=strval($angka); 
 	$tmp	="";
 	for($i=1; $i<=($panjang-strlen($inisial)-strlen($angka)); $i++) {
		$tmp=$tmp."0";	
	}
 	return $inisial.$tmp.$angka;
}

function buat_kombo($nama_tabel,$nama_kombo,$kelas,$order) {
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	print("<option value='' selected> - - Pilih ".$nama_kombo." - - </option>");
	$sql="select * from $nama_tabel order by $order";
	echo $sql;
	$query=mysql_query($sql);
	while($hasil=mysql_fetch_array($query)) {
		print("<option value=$hasil[0]>$hasil[1]</option>");
		}
	print("</select>");
}

function buat_kombo_baru($nama_tabel,$nama_kombo,$kelas,$kol_id,$kol_nama){
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	//print("<option value='' selected> - - Pilih ".$nama_kombo." - - </option>");
	$sql="select * from $nama_tabel ";
	//echo $sql;
	$query=mysql_query($sql);
	while($hasil=mysql_fetch_array($query)) {
		print("<option value=$hasil[$kol_id]>$hasil[$kol_nama]</option>");
		}
	print("</select>");
}

function buat_kombo_baru2($nama_tabel,$nama_tarif,$nama_kombo,$kelas,$kol_id1,$kol_id2,$kol_nama,$ppk){
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	$sql="select $kol_id1,$kol_nama from $nama_tabel";
	$query=mysql_query($sql);
	while($hasil=mysql_fetch_array($query)) {
		$cek = "select $kol_id2 from $nama_tarif where $kol_id2 = '$hasil[$kol_id1]' and id_ppk = $ppk";
		$jml = mysql_num_rows(mysql_query($cek));
		if($jml == 0){
			print("<option value=$hasil[$kol_id1]>$hasil[$kol_nama]</option>");
		}
	}
	print("</select>");
}

function kombo_kec($nama_kombo,$kelas,$order,$kecamatan) {
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	$sql="select * from kecamatan kc inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten inner join propinsi p on p.id_propinsi = kb.id_propinsi where kb.aktif = 'Y' and p.aktif = 'Y' order by $order";
	//echo $sql;
	$query=mysql_query($sql);
	while($hasil=mysql_fetch_array($query)) {
		if($hasil['id_kecamatan'] == $kecamatan){
			print("<option value=$hasil[0] selected>$hasil[2]</option>");
		}else{
			print("<option value=$hasil[0]>$hasil[2]</option>");
		}
	}
	print("</select>");
}

function edit_kombo_baru($nama_tabel,$nama_kombo,$kelas,$kol_id,$kol_nama,$hasil_val){
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	//print("<option value='' selected> - - Pilih ".$nama_kombo." - - </option>");
	$sql="select * from $nama_tabel ";
	//echo $sql;
	$query=mysql_query($sql);
	while($hasil=mysql_fetch_array($query)) {
		if($hasil_val == $hasil[$kol_id]){
			print("<option value=$hasil[$kol_id] selected>$hasil[$kol_nama]</option>");
		}else{
			print("<option value=$hasil[$kol_id]>$hasil[$kol_nama]</option>");
		}
	}
	print("</select>");
}

function edit_kombo_baru2($nama_tabel,$nama_kombo,$kelas,$kol_id,$kol_nama,$hasil_val){
	$sql="select * from $nama_tabel where $kol_id = $hasil_val";
	//print $sql;
	$query=mysql_query($sql);
	$hasil=mysql_fetch_array($query);
	print "<input type=text name='readonly' id='readonly' class='$kelas' value='$hasil[$kol_nama]' readonly='readonly'>";
}

function edit_kombo($nama_kombo,$tabel,$hasil,$kelas){
	print("<select name='$nama_kombo' id='$nama_kombo' class='$kelas'>");
	$tampil=mysql_query("select * from $tabel");
	while($temp=mysql_fetch_array($tampil)) {
		if($hasil == $temp[0]) {
			print("<option value=$temp[0] selected>$temp[1]</option>"); }
		else {
			print("<option value=$temp[0]>$temp[1]</option>"); }
		}
	print("</select>");
}

function kombo_from_array($nama_var,$kelas,$arr_data,$val_data=""){
	//array harus 1 dimensi
	echo "<select name='$nama_var' id='$nama_var' class='$kelas'>";
	foreach($arr_data as $key=>$val){
		if($key == $val_data) {
			echo "<option value='".$key."' selected>".$val."</option>"; 
		}else {
			echo "<option value='".$key."'>".$val."</option>"; 
		}
	}
	echo "</select>";
}

function kombo_from_array2($nama_var,$kelas,$arr_data,$val_data=""){
	//array harus 1 dimensi
	echo "<select name='$nama_var' id='$nama_var' class='$kelas'>";
	foreach($arr_data as $key=>$val){
		echo "<option value='".$key."'>".$val."</option>"; 
	}
	echo "</select>";
}

function format_angka($angka) {
	$hasil =  number_format($angka,0, ",",".");
	return $hasil;
}

function desimal($angka) {
	$hasil =  number_format($angka,2, ",",".");
	return $hasil;
}

function RandUnik($panjang) { 
   $pstring = "0123456789"; 
   $plen = strlen($pstring); 
      for ($i = 1; $i <= $panjang; $i++) { 
          $start = rand(0,$plen); 
          $unik.= substr($pstring, $start, 1); 
      } 

    return $unik; 
} 

function upload_file($file,$lokasi){
	$acak=rand(0000,9999);
	$lokasi_file=$_FILES[$file]['tmp_name'];
	$nama_file=$_FILES[$file]['name'];
	$nama_file_unik=$acak.$nama_file;
	if(move_uploaded_file($lokasi_file,$lokasi."$nama_file_unik")){
		return $nama_file_unik;
	}else{
		return false;
	}
}
$seminggu = array("Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
$hari = date("w");
$hari_ini = $seminggu[$hari];

$tgl_sekarang = date("Ymd");
$tgl_skrg     = date("d");
$bln_sekarang = date("m");
$thn_sekarang = date("Y");
$jam_sekarang = date("H:i:s");

$nama_bln=array(1=> "Januari", "Februari", "Maret", "April", "Mei", 
                    "Juni", "Juli", "Agustus", "September", 
                    "Oktober", "November", "Desember");

function terbilang($angka) {
    // pastikan kita hanya berususan dengan tipe data numeric
    $angka = (float)$angka;
     
    // array bilangan
    // sepuluh dan sebelas merupakan special karena awalan 'se'
    $bilangan = array(
            '',
            'satu',
            'dua',
            'tiga',
            'empat',
            'lima',
            'enam',
            'tujuh',
            'delapan',
            'sembilan',
            'sepuluh',
            'sebelas'
    );
     
    // pencocokan dimulai dari satuan angka terkecil
    if ($angka < 12) {
        // mapping angka ke index array $bilangan
        return $bilangan[$angka];
    } else if ($angka < 20) {
        // bilangan 'belasan'
        // misal 18 maka 18 - 10 = 8
        return $bilangan[$angka - 10] . ' belas';
    } else if ($angka < 100) {
        // bilangan 'puluhan'
        // misal 27 maka 27 / 10 = 2.7 (integer => 2) 'dua'
        // untuk mendapatkan sisa bagi gunakan modulus
        // 27 mod 10 = 7 'tujuh'
        $hasil_bagi = (int)($angka / 10);
        $hasil_mod = $angka % 10;
        return trim(sprintf('%s puluh %s', $bilangan[$hasil_bagi], $bilangan[$hasil_mod]));
    } else if ($angka < 200) {
        // bilangan 'seratusan' (itulah indonesia knp tidak satu ratus saja? :))
        // misal 151 maka 151 = 100 = 51 (hasil berupa 'puluhan')
        // daripada menulis ulang rutin kode puluhan maka gunakan
        // saja fungsi rekursif dengan memanggil fungsi terbilang(51)
        return sprintf('seratus %s', terbilang($angka - 100));
    } else if ($angka < 1000) {
        // bilangan 'ratusan'
        // misal 467 maka 467 / 100 = 4,67 (integer => 4) 'empat'
        // sisanya 467 mod 100 = 67 (berupa puluhan jadi gunakan rekursif terbilang(67))
        $hasil_bagi = (int)($angka / 100);
        $hasil_mod = $angka % 100;
        return trim(sprintf('%s ratus %s', $bilangan[$hasil_bagi], terbilang($hasil_mod)));
    } else if ($angka < 2000) {
        // bilangan 'seribuan'
        // misal 1250 maka 1250 - 1000 = 250 (ratusan)
        // gunakan rekursif terbilang(250)
        return trim(sprintf('seribu %s', terbilang($angka - 1000)));
    } else if ($angka < 1000000) {
        // bilangan 'ribuan' (sampai ratusan ribu
        $hasil_bagi = (int)($angka / 1000); // karena hasilnya bisa ratusan jadi langsung digunakan rekursif
        $hasil_mod = $angka % 1000;
        return sprintf('%s ribu %s', terbilang($hasil_bagi), terbilang($hasil_mod));
    } else if ($angka < 1000000000) {
        // bilangan 'jutaan' (sampai ratusan juta)
        // 'satu puluh' => SALAH
        // 'satu ratus' => SALAH
        // 'satu juta' => BENAR
        // @#$%^ WT*
         
        // hasil bagi bisa satuan, belasan, ratusan jadi langsung kita gunakan rekursif
        $hasil_bagi = (int)($angka / 1000000);
        $hasil_mod = $angka % 1000000;
        return trim(sprintf('%s juta %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else if ($angka < 1000000000000) {
        // bilangan 'milyaran'
        $hasil_bagi = (int)($angka / 1000000000);
        // karena batas maksimum integer untuk 32bit sistem adalah 2147483647
        // maka kita gunakan fmod agar dapat menghandle angka yang lebih besar
        $hasil_mod = fmod($angka, 1000000000);
        return trim(sprintf('%s milyar %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else if ($angka < 1000000000000000) {
        // bilangan 'triliun'
        $hasil_bagi = $angka / 1000000000000;
        $hasil_mod = fmod($angka, 1000000000000);
        return trim(sprintf('%s triliun %s', terbilang($hasil_bagi), terbilang($hasil_mod)));
    } else {
        return 'Wow...';
    }
}

function pembulatan($angka){
	
	$sisa=$angka%100;
	$bulat=$angka-$sisa;
	
	if($sisa>50){
		return $bulat+100;
	}else if(($sisa<=50)&&($sisa>0)){
		return $bulat+50;
	}else{
		return $bulat;
	}
}

//function buat penjumlahan tanggal
function tambah_tanggal($tgl_sekarang,$hari){
	//param tgl harus format yyyy-mm-dd
	$hasil=date('Y-m-d',strtotime('+'.$hari.' day',strtotime($tgl_sekarang)));
	return $hasil;
}

//function buat generate barcode

function cetak_barcode($kode){
	require_once "barcode.inc.php";
	$bar= new BARCODE();
	
	if($bar==false)
		die($bar->error());
	$bar->setSymblogy('CODE128');
	$bar->setHeight('50');
	//$bar->setFont("arial");
	$bar->setScale('2');
	$bar->setHexColor('#000000','#FFFFFF');
	$return = $bar->genBarCode($kode,"png","tmp_barcode/barcode_$kode");
}

function get_setting(){
	$sql="select * from setting where id_setting='1'";
	$qry=mysql_query($sql);
	$hasil=mysql_fetch_array($qry);
	return $hasil;
}

//buat array
function get_arr_query($sql){
	$query=mysql_query($sql);
	while ($hasil = mysql_fetch_assoc($query)){
		$result_array[] = $hasil;
	}
	return $result_array;
}

function get_arr_row($sql){
	$query=mysql_query($sql);
	while ($hasil = mysql_fetch_row($query)){
		$result_array[] = $hasil;
	}
	return $result_array;
}

//hitung klaim

function get_hitung_klaim($id){
	$sql="SELECT ifnull(SUM(total_tarif),0) AS total_tarif, ifnull(SUM(total),0) AS total FROM 
			(select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_layanan where id_klaim='".$id."'
		union ALL
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_tindakan where id_klaim='".$id."'
		union ALL
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_elektromedik where id_klaim='".$id."'
		union ALL
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_rehabilitasi where id_klaim='".$id."'
		union ALL
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_laboratorium where id_klaim='".$id."'
		union ALL
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_radiologi where id_klaim='".$id."'
		union ALL
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_obat where id_klaim='".$id."'
		union ALL
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_abhp where id_klaim='".$id."')AS data_raw";
	$qry=mysql_query($sql);
	//echo $sql;
	$hasil=mysql_fetch_array($qry);
	$selisih=$hasil['total_tarif']-$hasil['total'];
	return array('tarif_ditanggung'=>$hasil['total_tarif'],'tarif_diminta'=>$hasil['total'],'selisih'=>$selisih);
}

function hitung_lap_klaim($id_status,$id_jenis_layanan,$tgl1,$tgl2,$ppk,$jns){
	if($ppk != ''){
		$kd_ppk = "and k.id_ppk = $ppk";
	}else{
		$kd_ppk = "";
	}
	if($jns == 'service'){
		$sql="SELECT SUM(total_tarif) AS total_tarif,SUM(total)AS total FROM 
			(select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_layanan kl inner join klaim k on k.id_klaim = kl.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_tindakan kt inner join klaim k on k.id_klaim = kt.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_laboratorium klb inner join klaim k on k.id_klaim = klb.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_radiologi kr inner join klaim k on k.id_klaim = kr.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_obat ko inner join klaim k on k.id_klaim = ko.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_abhp ka inner join klaim k on k.id_klaim = ka.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns')AS data_raw";
		$qry=mysql_query($sql);
		//echo $sql;
		$hasil=mysql_fetch_array($qry);
		$selisih=$hasil['total_tarif']-$hasil['total'];
		return array('tarif_ditanggung'=>$hasil['total_tarif'],'tarif_diminta'=>$hasil['total'],'selisih'=>$selisih);
	}else{	
		$sql="SELECT SUM(total_tarif) AS total_tarif,SUM(total)AS total FROM 
			(select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_inacbg ki inner join klaim k on k.id_klaim = ki.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		union
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_obat ko inner join klaim k on k.id_klaim = ko.id_klaim where k.id_status=".$id_status." and k.id_jenis_layanan=".$id_jenis_layanan." and k.tgl_trans >= '$tgl1' and k.tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'
		)AS data_raw";
		
		$qry=mysql_query($sql);
		//echo $sql;
		$hasil=mysql_fetch_array($qry);
		$selisih=$hasil['total_tarif']-$hasil['total'];
		return array('tarif_ditanggung'=>$hasil['total_tarif'],'tarif_diminta'=>$hasil['total'],'selisih'=>$selisih);
	}
}

function hitung_pasien($id_status,$id_jenis_layanan,$tgl1,$tgl2,$ppk,$jns){
	if($ppk != ''){
		$kd_ppk = "and id_ppk = $ppk";
	}else{
		$kd_ppk = "";
	}
	
	$sql = mysql_query("select count(id_klaim) as jml, count(DateDiff(tgl_masuk,tgl_keluar)) as los from klaim where id_status=".$id_status." and id_jenis_layanan = ".$id_jenis_layanan." and tgl_trans >= '$tgl1' and tgl_trans <= '$tgl2' $kd_ppk and jenis_klaim = '$jns'");
	//echo $sql;
	$pasien = mysql_fetch_array($sql);
	return array('jml'=>$pasien['jml'],'los'=>$pasien['los']);
}

function get_hitung_inacbg($id){
	$sql="SELECT SUM(total_tarif) AS total_tarif,SUM(total)AS total FROM 
			(select sum(tarif)as total_tarif, sum(tarif_ditanggung) as total from klaim_inacbg where id_klaim='".$id."'
		union all
			select sum(tarif*jumlah)as total_tarif, sum(tarif_ditanggung*jumlah_ditanggung) as total from klaim_obat where id_klaim='".$id."'
		)AS data_raw";
	$qry=mysql_query($sql);
	$hasil=mysql_fetch_array($qry);
	$selisih=$hasil['total_tarif']-$hasil['total'];
	return array('tarif_ditanggung'=>$hasil['total_tarif'],'tarif_diminta'=>$hasil['total'],'selisih'=>$selisih);
}

function hapus_klaim($no_klaim){
	$hapus_obat=mysql_query("delete from klaim_obat where id_klaim='".$no_klaim."'");
	$hapus_ina=mysql_query("delete from klaim_inacbg where id_klaim='".$no_klaim."'");
	$hapus_prosedur=mysql_query("delete from klaim_prosedur where id_klaim='".$no_klaim."'");
	$hapus_diagnosis=mysql_query("delete from klaim_diagnosis where id_klaim='".$no_klaim."'");
	$hapus_klaim=mysql_query("delete from klaim where id_klaim='".$no_klaim."'");
}

function get_nama_ppk($id){
	$sql="select * from ppk where id_ppk='$id'";
	$qry=mysql_query($sql);
	$hasil=mysql_fetch_array($qry);
	return $hasil['nama'];
}

function hapus_data($id,$id_master,$id_relasi,$tabel_master,$tabel_relasi,$nama,$modul){
	$sql = "select m.$id_master from $tabel_master m inner join $tabel_relasi r on m.$id_master = r.$id_relasi where m.$id_master = $id group by m.$id_master";
	//echo $sql;
	$query = mysql_query($sql);
	$jml_data = mysql_num_rows($query);
	if($jml_data > 0){
		?>
			<a href="utama.php?module=<?php echo $modul;?>&view=pindah&id=<?php echo str_replace("'","","$id"); ?>" onclick="return confirm('Data <?php echo $nama; ?> ini sudah digunakan, apakah ingin dipindah dahulu sebelum dihapus ?')">
			<img src="asset/images/delete.png" title="Hapus Data"></a>
		<?php
	}else{
		?>
			<a href="utama.php?module=<?php echo $modul;?>&act=hapus&id=<?php echo str_replace("'","","$id"); ?>" onclick="return confirm('Anda yakin ingin Menghapus Data ?')">
			<img src="asset/images/delete.png" title="Hapus Data"></a>
		<?php
	}
}

function form_pindah($id,$id_master,$tabel_master,$nama,$modul,$tabel,$kelas){
	$sql="select * from $tabel_master where $id_master = $id";
	//echo $sql;
	$qry=mysql_query($sql);
	$hasil=mysql_fetch_array($qry);
	?>
	<form action="utama.php?module=<?php echo $modul;?>&act=hapus2" onsubmit="return confirm('Anda yakin akan memindah data ?')" method="post" enctype="multipart/form-data">
		<table style="margin: 10px 15px 10px 15px;">
			<tr>
				<td><?php echo $nama; ?></td>
				<td>&nbsp;</td>
				<td>
					<input name="nama" type="text" class="<?php echo $kelas; ?>" id="nama" value="<?php echo "$hasil[$tabel]";?>" disabled/>
					<input name="id" type="hidden" value="<?php echo $id;?>">
				</td>
			</tr>
			<tr>
				<td>Pindah ke</td>
				<td>&nbsp;</td>
				<td>
					<select name="transfer" id="transfer" class="<?php echo $kelas; ?>">
						<?php
							$data = mysql_query("select $id_master,$tabel from $tabel_master where $id_master <> $id order by $tabel");
							while($hasil = mysql_fetch_array($data)){
								echo "<option value=$hasil[$id_master]>$hasil[$tabel]</option>";
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td><input name="submit" type="submit" class="button" value="Proses" /></td>
			</tr>
		</table>
	</form>
	<?php
}

function hapus($id1,$id2,$id_master,$id_relasi,$tabel_master,$tabel_relasi,$modul){
	mysql_query ("update $tabel_relasi set $id_relasi = $id2 where $id_relasi = $id1");
	
	$sql="delete from $tabel_master where $id_master=$id1";
	$qry=mysql_query($sql);
	
	$url_sukses="utama.php?module=$modul";
	if($qry){
		echo "<meta http-equiv='refresh' content='0; url=$url_sukses'>";
		exit;
	}else{
		echo mysql_error();
	}
}

function hapus_data2($id,$id_master,$tabel_master,$tabel_relasi1,$tabel_relasi2,$modul){
	$sql1 = "select m.$id_master from $tabel_master m inner join $tabel_relasi1 r on m.$id_master = r.$id_master where m.$id_master = $id group by m.$id_master";
	$sql2 = "select m.$id_master from $tabel_master m inner join $tabel_relasi2 t on m.$id_master = t.$id_master where m.$id_master = $id group by m.$id_master";
	
	$query1 = mysql_query($sql1);
	$query2 = mysql_query($sql2);
	
	$jml_data1 = mysql_num_rows($query1);
	$jml_data2 = mysql_num_rows($query2);
	
	$jml_data = $jml_data1 + $jml_data2;

	if($jml_data > 0){
		?>
			<a href="utama.php?module=<?php echo $modul;?>&view=pindah&id=<?php echo str_replace("'","","$id"); ?>" onclick="return confirm('Data <?php echo $nama; ?> ini sudah digunakan, apakah ingin dipindah dahulu sebelum dihapus ?')">
			<img src="asset/images/delete.png" title="Hapus Data"></a>
		<?php
	}else{
		?>
			<a href="utama.php?module=<?php echo $modul;?>&act=hapus&id=<?php echo $id; ?>" onclick="return confirm('Anda yakin ingin Menghapus Data ?')">
			<img src="asset/images/delete.png" title="Hapus Data"></a>
		<?php
	}
}

function form_pindah2($id,$id_master,$tabel_master,$nama,$modul,$tabel,$kelas){
	?>
	<script type="text/javascript">
	$(document).ready(function(){
		$("#id_master").select2(); 
	});
	</script>
	<?php
	
	$sql="select * from $tabel_master where $id_master = $id";
	//echo $sql;
	$qry=mysql_query($sql);
	$hasil=mysql_fetch_array($qry);
	?>
	<form action="utama.php?module=<?php echo $modul;?>&act=hapus2" method="post" enctype="multipart/form-data" onsubmit="return confirm('Anda yakin akan memindah data ?')">
		<table style="margin: 10px 15px 10px 15px;">
			<tr>
				<td><?php echo $nama; ?></td>
				<td>&nbsp;</td>
				<td>
					<input name="nama" type="text" class="<?php echo $kelas; ?>" id="nama" value="<?php echo "$hasil[$tabel]";?>" disabled/>
					<input name="id" type="hidden" value="<?php echo str_replace("'","","$id");?>">
				</td>
			</tr>
			<tr>
				<td>Pindah ke</td>
				<td>&nbsp;</td>
				<td>
					<select name="transfer" class="<?php echo $kelas; ?>" id="id_master">
						<?php
							$data = mysql_query("select $id_master,$tabel from $tabel_master where $id_master <> $id order by $tabel");
							while($hasil = mysql_fetch_array($data)){
								echo "<option value=$hasil[$id_master]>$hasil[$tabel]</option>";
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td><input name="submit" type="submit" class="button" value="Proses" /></td>
			</tr>
		</table>
	</form>
	<?php
}

function hapus_medis($id1,$id2,$id_master,$tabel_master,$tabel_relasi1,$tabel_relasi2,$modul){
	mysql_query ("update $tabel_relasi1 set $id_master = $id2 where $id_master = $id1");
	mysql_query ("delete from $tabel_relasi2 where $id_master = $id1");
	
	$sql="delete from $tabel_master where $id_master=$id1";
	$qry=mysql_query($sql);
	//echo $sql;
	$url_sukses="utama.php?module=$modul";
	if($qry){
		echo "<meta http-equiv='refresh' content='0; url=$url_sukses'>";
		exit;
	}else{
		echo mysql_error();
	}
}

function delete_folder($path){
	//tanpa tanda /
	$files = glob($path.'/*'); // get all file names
	if(!empty($files)){
		foreach($files as $file){ // iterate files
			if(is_file($file))
			unlink($file); // delete file
		}
	}
}
function get_detail_ppk_userlogin($id_user){
	$sql="select b.id_ppk,a.id_pengguna,a.nama as nama_user,b.nama AS nama_ppk  from pengguna a inner join ppk b on a.id_ppk=b.id_ppk 
	where a.id_pengguna='".$id_user."'";
	//echo $sql;
	$qry=mysql_query($sql);
	return mysql_fetch_array($qry);
}
?>